<?php
namespace M\Controller;
use Think\Controller;

class InformationController extends CommonController {

	/**
	 * 常见问题
         * 
         * 
         * test
	 * @return [type] [description]
	 */
	public function faq_lists(){

		if(IS_GET){

			$DB_news = M('news');

			$news_map['news_type'] = 2;
                        
                        $from_index = I("get.from_index");
                        if(empty($from_index) || $from_index==0)
                        {
                            $this->assign("from_index", 0);
                        }
                        else
                        {
                            $this->assign("from_index", 1);
                        }

			$list = $DB_news->where($news_map)->select();

			foreach ($list as $key => $value) {
				# code...
				$list[$key]['content'] = htmlspecialchars_decode($value['content']);
			}
			// print_r($list); die;
			$this->assign('list',$list);
			$this->display();
		}
	}

	/**
	 * 问题提问反馈
	 */
	public function faq_feedback(){

		if(IS_GET){

			$this->display();

		}else if(IS_POST){

			$content = I('post.content');
			if(!empty($content)){

				$data['content'] = $content;
				$data['ctime'] = I('server.REQUEST_TIME');
				$data['mid'] = $this->memberinfo['id'];
				$data['feedback_type'] = 2;
			}
			print_r($data); die;
			$DB_feedback = M('feedback');

			$res = $DB_feedback->data($data)->add();

			if(!empty($res)){

				echo json_encode(1);
			}else{

				echo json_encode(0);
			}
		}
	}



	/**
	 * 危害须知列表
	 */
	public function harm_lists(){


		if(IS_GET){

			$DB_news = M('news');

			$news_map['news_type'] = 1;

			$list = $DB_news->where($news_map)->select();

			foreach ($list as $key => $value) {
				# code...
				$list[$key]['content'] = htmlspecialchars_decode($value['content']);
			}
			// print_r($list); die;
			$this->assign('list',$list);
			$this->display();
		}
	}

	/**
	 * 危害须知详情内容
	 */
	public function harm_details(){

		if(IS_GET){

			$id = I('get.id');
			if(!empty($id)){

				$news_map['id'] = $id;
			}

			$DB_news = M('news');

			$list = $DB_news->where($news_map)->find();

			$list['content'] = htmlspecialchars_decode($list['content']);

			$this->assign('list',$list);
			$this->display();
		}
	}



	/**
	 * 食事周刊列表
	 */
	public function weekly_lists(){


		if(IS_GET){

			$DB_news = M('news');

			$news_map['news_type'] = 3;

			$news_map['active'] = 1;

			$list = $DB_news->where($news_map)->order('rank desc')->select();
                        
                        foreach ($list as $key => $value) {
                            //实时计算评论数量
                            $list[$key]['comment_count'] = M("comments")->where(array('for_id'=>$value['id'],'ctype'=>6,'ishidden'=>0))->count();
                        }

			// print_r($list); die;
			$this->assign('list',$list);
			$this->display();
		}
	}


	/**
	 * 食事周刊详情内容
	 */
	public function weekly_details(){


		if(IS_GET){

			$id = I('get.id');
			if(!empty($id)){

				$news_map['id'] = $id;
			}

			$DB_news = M('news');

			$list = $DB_news->where($news_map)->find();

			$list['content'] = htmlspecialchars_decode($list['content']);

			$hot_comments = $this->get_hot_comments($id,6);

			$all_comments_count = $this->get_all_comments_count($id,6);

			//查看该文章是否被收藏
			$iscollection = $this->chk_is_collection($id,6,1);
			// echo $iscollection;die; 
			// print_r($hot_comments); die;
			$this->assign('list',$list);
			$this->assign('all_comments_count',$all_comments_count);
			$this->assign('hot_comments',$hot_comments);
			$this->assign('iscollection',$iscollection);
			$this->display();
		}
	}

	/**
	 * 查询文章是否被收藏
	 */
	private function chk_is_collection($for_id,$ctype,$mid){

		$map['for_id'] = $for_id;

		$map['ctype'] = $ctype;

		$map['mid'] = $mid;

		$DB_collections = M('collections');

		$result = $DB_collections->where($map)->find();

		if(!empty($result)){

			return 1;
		}else{

			return 0;
		}
	}
	


	/**
	 * 食事周刊评论表
	 */
	public function weekly_comments(){

		if(IS_GET){

			$id = I('get.id');

			if(!empty($id)){

				$for_id = $id;
			}

			$all_comments = $this->get_all_comments($for_id,6);

			$this->assign('id',$id);
			$this->assign('all_comments',$all_comments);
			$this->display();
		}
	}




	/**
	 * 获取热门评论10条
	 */
	
	private function get_hot_comments($for_id,$ctype){

		$DB_comments = M('comments');

		$sql = 'select fd_comments.*,fd_members.username as username,fd_members.avatar as avatar from fd_comments left join fd_members on fd_comments.mid = fd_members.id where fd_comments.for_id='.$for_id.' and fd_comments.ctype ='.$ctype.' and fd_comments.ishidden = 0'.' order by likecount desc,comment_count desc limit 0,10';

		$result = $DB_comments->query($sql);
                
                foreach ($result as $key => $value) {
                    //获取二级评论
                    $child_comments = $this->_get_sencond_comments($value['id']);
                    $result[$key]['child_comments']= $child_comments;
                    $result[$key]['child_comment_count']=count($child_comments);
                }
		
		return $result;
	}



	/**
	 * 获取评论的条目数
	 */
	private function get_all_comments_count($for_id,$ctype){

		$DB_comments = M('comments');

		$map['for_id'] = $for_id;
		$map['ctype'] = $ctype;
		$map['ishidden'] = 0;
		$map['pid'] = 0;

		$res = $DB_comments->where($map)->count();

		return $res;
	}

	/**
	 * 添加评论
	 */
	
	public function add_commnet(){

		$data['for_id'] = I('post.for_id');
		$data['ctype'] = I('post.ctype');
		$data['content'] = I('post.content');
		$data['ctime'] = I('server.REQUEST_TIME');
        $data['ishidden'] = 0;
		$data['mid'] = $this->memberinfo['id'];

		$DB_comments = M('comments');

		//检测评论是否包含敏感词
		$chk_back = chk_bad_words($data['content'],1);
		if($chk_back){
			echo json_encode(2);
		}else{
			$result = $DB_comments->data($data)->add();

			if(!empty($result)){

				echo json_encode(1);
			}
		}
		
	}

	/**
	 * 获取到所有的一级评论列表
	 */
	private function get_all_comments($for_id,$ctype){

		$map['for_id'] = $for_id;
		$map['ctype'] = 6;
		$map['ishidden'] = 0;

		$DB_comments = M('comments');

		$sql = 'select fd_comments.*,fd_members.username as username,fd_members.avatar as avatar from fd_comments left join fd_members on fd_comments.mid = fd_members.id where fd_comments.for_id='.$for_id.' and fd_comments.ctype ='.$ctype.' and fd_comments.ishidden = 0'.' order by likecount desc,comment_count desc';
		$res = $DB_comments->query($sql);

		return $res;
	}



	/*
     * 
     * 项目的评论点赞
     * 
     */
    public function comments_like_add() {
        $mid = I('get.mid');

        if($mid)
        {
            $cid = I("get.cid");


            $data['cid']=$cid;
            $data['mid']=$mid;
            $data['ctime']=I("server.REQUEST_TIME");
            
            $DB = M("comment_likes");
            
            $id = $DB->add($data);
            
            if($id)
            {
                $DB_comment = M("comments");
                $DB_comment->where("id=".$cid)->setInc("likecount");                
                echo 1;
            }
        }
        else
        {
            echo 0;
        }
    }



    /**
     * 二级评论页面
     */
    public function child_comments(){

    	if(IS_GET){

    		$id = I('get.id');

    		$DB_comments = M('comments');

    		$sql1 = 'select fd_comments.*,fd_members.username as username,fd_members.avatar as avatar from fd_comments left join fd_members on fd_comments.mid = fd_members.id where fd_comments.id='.$id;

    		$sql2 = 'select fd_comments.*,fd_members.username as username,fd_members.avatar as avatar from fd_comments left join fd_members on fd_comments.mid = fd_members.id where fd_comments.pid='.$id;

    		$parent = $DB_comments->query($sql1);

    		$child = $DB_comments->query($sql2);
    		// print_r($parent); die;
    		// print_r($child); die;
    		$this->assign('parent',$parent);
    		$this->assign('child',$child);
    		$this->assign('id',$id);
    		$this->display();
    	}
    }



    /**
     * 对一级评论进行评论
     */
    public function add_child_commnet(){

    	$data['pid'] = I('post.id');
		$data['ctype'] = I('post.ctype');
		$data['content'] = I('post.content');
		$data['ctime'] = I('server.REQUEST_TIME');
		$data['mid'] = $this->memberinfo['id'];

		$DB_comments = M('comments');

		$chk_back = chk_bad_words($data['content'],1);
		if($chk_back){

			echo json_encode(2);
		}else{
			$result = $DB_comments->data($data)->add();
		
			if(!empty($result)){
				send_msg_to_goal_user($result);
				echo json_encode(1);
			}
		}
    }



    /*
     * 针对项目的收藏功能
     */
    
    public function add_one_colletion() {

    	$iscollection = I('post.iscollection');

    	$DB_collections = M('collections');

 

    	if($iscollection == 1){

    		$data['ctype'] = I('post.ctype');
	    	$data['for_id'] = I('post.for_id');
	    	$data['mid'] = 1;
                $data['ctime']=I("server.REQUEST_TIME");

    		$res = $DB_collections->data($data)->add();

    		if(!empty($res)){

    			echo json_encode(1);
    		}

    	}else if($iscollection == 0){

    		$map['ctype'] = I('post.ctype');
	    	$map['for_id'] = I('post.for_id');
	    	$map['mid'] = 1;

	    	$res = $DB_collections->where($map)->delete();

	    	if($res){

	    		echo json_encode(1);
	    	}
    	}
       
  	}


  	/**
  	 * 食事周刊详情
  	 */
  	public function details_for_share(){

  		$id = I('get.id');
  		if(!empty($id)){

  			$map['id'] = $id;
  		}

  		$DB_news = M('news');

  		$list = $DB_news->where($map)->find();

  		//对内容进行实体化
  		$list['content'] = htmlspecialchars_decode($list['content']);

  		$this->assign('list',$list);
  		$this->display();
  	}

}
?>